package com.example.hotelapp.Activity;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotelapp.Adapter.DetailAdapter;
import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Bean.ModifyPwdBean;
import com.example.hotelapp.Bean.RoomDetailBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.MyCommonUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.ToastShow;
import static com.example.utils.CommonUtils.Delay3s;
import static com.example.utils.CommonUtils.DialogShow;
import static com.example.utils.CommonUtils.OKPostValue;
import static com.example.utils.CommonUtils.SharedInfo;
import static com.example.utils.CommonUtils.setSystemBarColor;

/**
 * 修改密码页
 */
public class ModifyPwdActivity extends BaseActivity {

    @BindView(R.id.user_password)
    EditText userPassword;
    @BindView(R.id.user_password2)
    EditText userPassword2;
    private Activity activity;

    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.activity_modifypwd;
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = this;
        setSystemBarColor(this, R.color.white);

    }


    /**
     * 点击事件
     */
    @OnClick({R.id.iv_fanhui, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.btn_ok:
                ModifyActivity();
                break;
        }
    }

    /**
     * 修改密码
     */
    private void ModifyActivity() {
        String queren1 = userPassword.getText().toString().trim();
        String queren2 = userPassword2.getText().toString().trim();
        String id = SharedInfo(activity,"GetUid","id");
        if (queren1.isEmpty() || queren2.isEmpty()){
            ToastShow(activity,"请填写完整信息");
        }else if (!queren1.equals(queren2)){
            ToastShow(activity,"密码不一致");
        }else {
            OKPostValue("id", id, "password", queren1, AppNetConfig.ModifyPwd, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastShow(activity,"服务器出错，请稍后再试");
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    //获取返回的json数据并解析
                    String json = response.body().string();
                    System.out.println(json);
                    ModifyPwdBean databean = new Gson().fromJson(json, ModifyPwdBean.class);
                    int code = databean.getCode();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code == 1) {
                                ToastShow(activity,"修改成功");
                                Delay3s(activity);
                            } else {
                                ToastShow(activity,"修改失败");
                            }
                        }
                    });
                }
            });
        }
    }
}
